package com.funtl.framework.java.util.net.httpclient;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

/**
 * 提取HTML数据
 * @author lusifer
 *
 */
public class ClientParser {
	
	/**
	 * 解析HTML
	 * @param resource	HTML文本
	 * @param filter	过滤条件
	 * @return
	 * @throws ParserException
	 */
	public static String parser(String resource, NodeFilter filter) throws ParserException {
		StringBuffer sb = new StringBuffer();
		Parser parser = new Parser(resource);
		NodeList nodes = parser.extractAllNodesThatMatch(filter);
		
		if (nodes != null) {
			for (int i = 0; i < nodes.size(); i++) {
				Node textnode = (Node) nodes.elementAt(i);
				sb.append(textnode.toPlainTextString());
			}
		}
		
		return sb.toString();
	}
	
}
